<?php
namespace app\manage\controller;

use app\manage\controller\BaseController;

/**
 * @author 李全春 <adv30@126.com>
 * 所有接口的入口类
 */
class Index extends BaseController
{
    // 允许此域名发起的跨域请求
    private $arrow = [
        'null',
        'http://localhost:8080',
        'http://newthink.local',
        'http://www.newthink.local',
    ];

    // 输出跨域的头
    private function _echoHeader(){
        $httpOrigin = input('server.HTTP_ORIGIN');
        
        if (APP_DEBUG) {
            header('Access-Control-Allow-Origin: ' . $httpOrigin);
            header('Access-Control-Allow-Credentials: true');
        } else if (in_array($httpOrigin, $this->arrow)){
            header('Access-Control-Allow-Origin: ' . $httpOrigin);
            header('Access-Control-Allow-Credentials: true');
        }
    }
    // 默认接口访问页
    public function index()
    {
        $this->_echoHeader();
        $apiService = service('Api');     //生成调度对象,接收参数\数据,并进行检查
        $apiService->run(); //调用接收到的action,并执行,且返回json数据
        return $apiService->ajaxReturn(); //返回执行的数据
    }

    // 获取当前的app分类树
    public function getCate(): array
    {
        $this->_echoHeader();
        // 获取分类树
        $tree = logic('ApiCate')->getCateTree();
        return auto_return($tree, '找到分类树', '没有数据');
    }
    // 获取总接口数量及各状态数量
    public function getCount(): array
    {
        $this->_echoHeader();
        $res = logic('Api')->getCountData();
        return auto_return($res, '找到统计数据', '没有数据');
    }
    // 获取该分类下的所有可用api
    public function cateApis($id = 0): array
    {
        $this->_echoHeader();
        // $data['api_cate_id'] = input('request.id');
        $data['api_cate_id'] = $id;
        $validate = validate('Api');
        if(!$validate->check($data)){
            return verify_err($data, $validate->getError());
        }
        $res = logic('Api')->getCateApis($data);
        return auto_return($res, '找到接口列表', '没有数据');
    }

    // 获取允计创建的版本
    public function getArrowVer(): array
    {
        $this->_echoHeader();
        $res = config('arrow_make_server');
        return auto_return($res);
    }
    // 创建api分类
    public function createApiCate(): array
    {
        $this->_echoHeader();
        if (!EDIT_API) {
            return verify_err([], '接口不可编辑!');
        }
        $data['name'] = input('post.name');
        $data['glyphicons'] = input('post.glyphicons');
        $data['parent_id'] = input('post.parent_id');
        $data['level'] = input('post.level');
        $data['action'] = input('post.action');
        $data['ver'] = input('post.ver');
        $data['status'] = input('post.status') === 'true' ? '1' : '0';
        $data['order'] = input('post.order');
        $validate = validate('ApiCate');
        if(!$validate->check($data)){
            return verify_err($data, $validate->getError());
        }
        $res = logic('ApiCate')->addCate($data);
        return auto_return($res);
    }
    // 禁用一个api分类
    public function removeCate (): array
    {
        $this->_echoHeader();
        if (!EDIT_API) {
            return verify_err([], '接口不可编辑!');
        }
        $data['id'] = input('post.id');
        $validate = validate('ApiCate');
        if(!$validate->check($data)){
            return verify_err($data, $validate->getError());
        }
        $res = logic('ApiCate')->removeCate($data);

        return auto_return($res);
    }
    // 增加api接口
    public function addApi (): array
    {
        $this->_echoHeader();
        if (!EDIT_API) {
            return verify_err([], '接口不可编辑!');
        }
        $data = input('get.');
        $validate = validate('Api');
        if(!$validate->check($data)){
            return verify_err($data, $validate->getError());
        }
        $res = logic('Api')->addApi($data);
        return auto_return($res);
    }
    // 删除一个接口
    public function removeApi (): array
    {
        $this->_echoHeader();
        if (!EDIT_API) {
            return verify_err([], '接口不可编辑!');
        }
        $data['id'] = input('get.id');
        $validate = validate('Api');
        if(!$validate->check($data)){
            return verify_err($data, $validate->getError());
        }
        $res = logic('Api')->removeApi($data);
        return auto_return($res);
    }
    // 获取一个token
    public function getToken (): array
    {
        $this->_echoHeader();
        return \app\manage\service\ApiBase::getToken();
    }

    public function upload()
    {
        $this->_echoHeader();
        $file = new \app\serverfile\service\file\ApiFile();
        return auto_return($file->upload());
    }
}
